package cn.kxx.controller;


import cn.kxx.pojo.*;
import cn.kxx.service.OrderService;
import cn.kxx.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: kxx
 * @Date: 14:34
 * @Desc: 产品操作
 */
@Controller
@RequestMapping("user")
public class UserController {

   @Autowired
   private UserService userService;

   /**
    *@描述: 查询所有用户信息
    *@参数: page size
    *@返回值:  list
    *@创建人: kxx
    *@创建时间: 2022/11/3 16:13
    *@修改人和其它信息:
    */
   @RequestMapping("findAll")
   private ModelAndView findAll(@RequestParam(value = "page",defaultValue = "1") Integer page,
                          @RequestParam(value = "size",defaultValue = "5") Integer size){

      PageHelper.startPage(page,size);
      List<UserInfo> list = userService.findAll();
      PageInfo pageInfo = new PageInfo(list);

      ModelAndView mv = new ModelAndView();
      mv.addObject("pageInfo",pageInfo);
      mv.setViewName("user-list");
      return mv;
   }


       // <!--新增user信息-->
   @RequestMapping("save")
   private String save(UserInfo userInfo){

      userService.save(userInfo);
      return "redirect:/user/findAll.do";

   }



   /**
    * 用户详情
    * @return
    */
   @RequestMapping("findById")
   public String findById(Integer id,HttpServletRequest request){

      HttpSession session = request.getSession();
      UserInfo userInfo = userService.findById(id);
      session.setAttribute("user",userInfo);

      return "redirect:/pages/user-show.jsp";
   }

   /**
    *@描述: 给用户添加角色
    *@参数:
    *@返回值:
    *@创建人: kxx
    *@创建时间: 2022/11/4 19:20
    *@修改人和其它信息:
    */

   @RequestMapping("findUserByIdAndAllRole")
   public ModelAndView findUserByIdAndAllRole(Integer id,HttpServletRequest request){


      HttpSession session = request.getSession();
      session.setAttribute("user1",id);
      List<Role> roleList = userService.findUserByIdAndAllRole(id);

     ModelAndView mv = new ModelAndView();
     mv.setViewName("user-role-add");
     mv.addObject("roleList",roleList);
     return mv;
   }
   @RequestMapping("addRoleToUser")
   public String addRoleToUser(@RequestParam("userId") Integer userId,@RequestParam("ids")Integer[] ids){

      userService.addRoleToUser(userId,ids);


      return "redirect:/user/findAll.do";
   }


   @RequestMapping("deleteUser")
   public String deleteUser(int id){

      userService.deleteUser(id);


      return "redirect:/user/findAll.do";
   }
   //选中删除
   @RequestMapping("selectDelete")
   public String selectDelete(String ids){

      String[] split = ids.split(",");
      userService.selectDelete(split);


      return "redirect:/user/findAll.do";
   }


}











